package StackAndQueuePackage;

import java.util.Stack;

/**
 * @author Lzm
 * @version 1.0
 */
public class evalRPN_ {

  public int evalRPN(String[] tokens) {
    Stack<String> stack = new Stack<>();
    for (int i = 0 ; i < tokens.length ; i++){
      if (tokens[i].equals("+")){
        int num1 = Integer.parseInt(stack.pop());
        int num2 = Integer.parseInt(stack.pop());
        int result = num1 + num2;
        stack.push(String.valueOf(result));
      }
      else if (tokens[i].equals("-")){
        int num1 = Integer.parseInt(stack.pop());
        int num2 = Integer.parseInt(stack.pop());
        int result = num1 - num2;
        stack.push(String.valueOf(result));
      }
      else if(tokens[i].equals("*")){
        int num1 = Integer.parseInt(stack.pop());
        int num2 = Integer.parseInt(stack.pop());
        int result = num1 * num2;
        stack.push(String.valueOf(result));
      }
      else if(tokens[i].equals("/")){
        int num1 = Integer.parseInt(stack.pop());
        int num2 = Integer.parseInt(stack.pop());
        int result = num1 / num2;
        stack.push(String.valueOf(result));
      }
      else{
        stack.push(tokens[i]);
      }
    }
    return Integer.parseInt(stack.pop());
  }
}
